package org.finesys.system.service.impl;

import java.time.LocalDateTime;

import org.finesys.common.mp.core.service.impl.BaseServiceImpl;
import org.finesys.system.api.dto.SysUserDTO;
import org.finesys.system.api.dto.ThirdBindDto;
import org.finesys.system.api.dto.UserGiteeDto;
import org.finesys.system.api.entity.SysUserGitee;
import org.finesys.system.api.vo.UserVO;
import org.finesys.system.constants.ThirdTypeEnums;
import org.finesys.system.convert.SysUserConvert;
import org.finesys.system.convert.SysUserGiteeConvert;
import org.finesys.system.mapper.SysUsersGiteeMapper;
import org.finesys.system.service.SysThirdOperateService;
import org.finesys.system.service.SysUsersGiteeService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class SysUsersGiteeServiceImpl extends BaseServiceImpl<SysUsersGiteeMapper, SysUserGitee> implements SysUsersGiteeService, SysThirdOperateService {

    private final SysUsersGiteeMapper sysUsersGiteeMapper;

    @Override
    public Boolean addSysUsersGitee(UserGiteeDto sysUsersGiteeDTO) {
        SysUserGitee sysUserGitee = SysUserGiteeConvert.INSTANCE.convert(sysUsersGiteeDTO);
        return this.save(sysUserGitee);
    }

    @Override
    public Boolean update(UserGiteeDto sysUsersGiteeDTO) {
        SysUserGitee sysUserGitee = SysUserGiteeConvert.INSTANCE.convert(sysUsersGiteeDTO);
        return this.updateById(sysUserGitee);
    }

    @Override
    public Boolean deleteGitee(Long id) {
        return this.removeById(id);
    }

    @Override
    public UserGiteeDto findById(Long id) {
        SysUserGitee sysUserGitee = sysUsersGiteeMapper.selectById(id);
        return SysUserGiteeConvert.INSTANCE.convert(sysUserGitee);
    }


    @Override
    public UserGiteeDto getByAppidAndId(String appId, Integer id) {
        SysUserGitee sysUserGitee = sysUsersGiteeMapper.selectOne(Wrappers.<SysUserGitee>lambdaQuery().eq(SysUserGitee::getAppId, appId).eq(SysUserGitee::getId, id));
        return SysUserGiteeConvert.INSTANCE.convert(sysUserGitee);
    }

    @Override
    public Boolean binding(UserGiteeDto sysUsersGiteeDTO) {
        SysUserGitee sysUserGitee = SysUserGiteeConvert.INSTANCE.convert(sysUsersGiteeDTO);
        LambdaUpdateWrapper<SysUserGitee> wrapper = Wrappers.<SysUserGitee>lambdaUpdate().eq(SysUserGitee::getId, sysUserGitee.getId())
                .eq(SysUserGitee::getAppId, sysUserGitee.getAppId()).set(SysUserGitee::getUserId, sysUserGitee.getUserId())
                .set(SysUserGitee::getBindingDate, LocalDateTime.now());
        return sysUsersGiteeMapper.update(sysUserGitee, wrapper) > 0;
    }

    @Override
    public SysUserDTO getUser(String appId, Integer id) {
        UserVO userVo = sysUsersGiteeMapper.getUser(appId, id);
        SysUserDTO sysUserDTO = SysUserConvert.INSTANCE.convert(userVo);
        if (CollUtil.isNotEmpty(userVo.getPermsList())) {
            String perms = CollUtil.join(userVo.getPermsList(), StrUtil.COMMA);
            sysUserDTO.setPerms(perms);
        }
        return sysUserDTO;
    }

    @Override
    public String support() {
        return ThirdTypeEnums.GITEE.getValue();
    }

    @Override
    public ThirdBindDto getBindInfo(Long userId) {
        SysUserGitee sysUserGitee = sysUsersGiteeMapper.selectOne(Wrappers.<SysUserGitee>lambdaQuery().eq(SysUserGitee::getUserId, userId));
        ThirdBindDto.ThirdBindDtoBuilder thirdBindDtoBuilder = ThirdBindDto.builder();
        thirdBindDtoBuilder.userId(userId).userName(sysUserGitee.getGiteeName()).bind(true).type(ThirdTypeEnums.GITEE.getValue());
        return thirdBindDtoBuilder.build();
    }

    @Override
    public boolean unbind(Long userId) {
        SysUserGitee sysUserGitee = new SysUserGitee();
        sysUserGitee.setUserId(userId);
        return sysUsersGiteeMapper.update(sysUserGitee, Wrappers.<SysUserGitee>lambdaUpdate().eq(SysUserGitee::getUserId, userId).set(SysUserGitee::getUserId, null)) > 0;
    }
}
